package Boke2;

public class SameLoker {
    //创建两个用于线程加锁的对象
    public static Object loker1=new Object();
    public static Object loker2=new Object();

    public static void main(String[] args) {

        //新线程t1
        Thread t1=new Thread(()->{
            System.out.println("t1线程开始 调用wait之前");
            //让t1线程进入阻塞等待
            synchronized (loker1){
                try {
                    //让被加锁的loker1对象进入阻塞等待
                    loker1.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t1被唤醒线程结束 wait阻塞已解除");
        });
        t1.start();

        //新线程t2
        Thread t2=new Thread(()->{
            System.out.println("t2线程开始 调用wait之前");
            //让t1线程进入阻塞等待
            synchronized (loker1){
                try {
                    //让被加锁的loker1对象进入阻塞等待
                    loker1.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t2被唤醒线程结束 wait阻塞已解除");
        });
        t2.start();

        //创建t3线程用于唤醒t1或t2线程
        Thread t3=new Thread(()->{
            try {
                //先让t3休眠较长时间让线程t1和t2进入阻塞等待
                Thread.sleep(1500);
                //必须先对同一个对象加锁(loker1)
                synchronized (loker1){
                    try {
                        System.out.println("t3线程启动：唤醒t1或t2线程中");
                        Thread.sleep(500);
                        //唤醒线程t1或t2
                        //loker1.notifyAll();
                        loker1.notify();
                        //loker1.notify();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        });
        t3.start();
    }
}
